package com.bank;

import android.graphics.Bitmap;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;

import com.bank.GOME_M27A.M27ABCbank;
import com.bank.lenovoz5.Z5ABCbank;
import com.entity.PointBean;
import com.entity.User;
import com.show.api.util.StringUtils;
import com.utils.ChatClient;
import com.utils.CodeUtils;
import com.utils.Constants;
import com.utils.FileHelper;
import com.utils.ImageView;
import com.utils.LogToFile;
import com.utils.UIUtils;
import com.utils.Util;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static android.os.SystemClock.sleep;
import static com.utils.ChatClient.CCBMethodName;
import static com.utils.ChatClient.C_updateBalance_W;
import static com.utils.ChatClient.ConfirmOrder;
import static com.utils.ChatClient.PAUSE;
import static com.utils.ChatClient.S_paused_C;
import static com.utils.ChatClient.addWorkTime;
import static com.utils.ChatClient.getmap;
import static com.utils.ChatClient.list;
import static com.utils.ChatClient.mapDelete;
import static com.utils.ChatClient.pressu;
import static com.utils.ChatClient.setDevicesLog;
import static com.utils.ChatClient.user;
import static com.utils.ChatClient.userInfo;
import static com.utils.Constants.orderBankName;
import static com.utils.ImageView.saveImg22;

/**
 * 中国农业银行自动化脚本
 */
@RunWith(AndroidJUnit4.class)
public class ABCbank extends Bank {
    private static final String ZZTAG = "转账(转支)";
    //转账下一步按钮
    private static int[] mBT_next = new int[]{370, 913};
    private static String info = "";
    private static boolean isKHHDialog = true; //是否有大额转账的开户行弹框
    private static UiObject2 targetLayout = null;
    private boolean isNewU = false;//是新型U盾

    static {
        BANK_FLAG = "ABC";
        BANK_NAME = "中国农业银行";
        carrier = android.os.Build.MODEL;//机型
        smsCode = "95599";
        context = InstrumentationRegistry.getTargetContext();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        ratio = screenWidth / 720f;
        ratioH = screenHeight / 1080f;
        Constants.CODE = "";
    }

    @Test
    public void demo() throws UiObjectNotFoundException {
        LogToFile.init(context, Constants.PROT);
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        if (!android.os.Build.MODEL.contains("SM-C710F") && !android.os.Build.MODEL.contains("2017M27A")) {
            UIUtils.initServicePermission(mDevice);
        }
        mDevice.pressHome();
        mDevice.pressHome();
        sleep(ChatClient.overtime_1);
        Constants.tryingToReadAccountInformation(mDevice, BANK_NAME);
        //启动银行app
        UiObject x = mDevice.findObject(new UiSelector().text(BANK_NAME));
        showMsg("进入" + BANK_NAME + "银行");
        x.click();
        showMsg("手机分辨率" + carrier + "-----" + screenWidth + "----" + screenHeight + "----" + ratio + "----" + ratioH);
        Constants.getsTheModelAndVersionNumberAndVersionName(context, BANK_NAME);
        //动态注册短信广播
        CodeUtils.initSMSReceiver(context);
        if (android.os.Build.MODEL.equals("Lenovo L78011") || android.os.Build.MODEL.contains("Lenovo L38041") || android.os.Build.MODEL.contains
                ("Lenovo L58041")) {
            showMsg("进入联想:" + android.os.Build.MODEL + "_设备");
            new Z5ABCbank(this);
            return;
        } else if (android.os.Build.MODEL.contains("2017M27A")) {
            showMsg("金立M27设备");
            new M27ABCbank(this);
            return;
        }
        //检查usb连接状态
        while (true) {
            if (Constants.STATE_USB) {
                showMsg("开启界面监听");
                transfer();
                break;
            }
        }
    }

    //检索转账：进入转账
    public void transfer() throws UiObjectNotFoundException {
        //重新启动银行app
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_8, "检测重启超时", Constants.notReport);
            if (isOutTime) {
                break;
            }
            // root手机需要点击继续使用
            UiObject keepUsing = mDevice.findObject(new UiSelector().resourceId("android:id/button2").text("继续使用"));
            if (keepUsing.exists()) {
                try {
                    showMsg("===root提示框取消===");
                    keepUsing.click();
                    sleep(ChatClient.overtime_2);
                } catch (UiObjectNotFoundException e) {
                    e.printStackTrace();
                }
            }

            UiObject x = mDevice.findObject(new UiSelector().text("中国农业银行").text("android.widget.TextView"));
            if (x.exists()) {
                showMsg("重新启动农行");
                x.click();
                sleep(ChatClient.overtime_3);
                transfer();
                return;
            }
        }

        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_15, "登陆前，检测版本更新超时", Constants.notReport);
            if (isOutTime) {
                break;
            }
            /************处理引导页**************/
            UiObject mLL = mDevice.findObject(new UiSelector().resourceId("com.android.bankabc:id/indicator"));
            if (mLL.exists()) {
                ChatClient.timer(LogToFile.getMethodName(), "引导页重启");
                showMsg("引导页，滑动");
                mDevice.drag(900, 600, 100, 600, 10);
                mDevice.click(405, 555);
                sleep(ChatClient.overtime_1);
            }

            UiObject x = mDevice.findObject(new UiSelector().text("中国农业银行").text("android.widget.TextView"));
            if (x.exists()) {
                showMsg("重新启动农行");
                x.click();
                sleep(ChatClient.overtime_3);
                transfer();
                return;
            }

            UiObject but1 = CodeUtils.getUiObject_textOrId("暂不更新", "com.yitong.mbank.psbc:id/dialog_cancel_tv");
            UiObject but2 = CodeUtils.getUiObject_textOrId("取消", "android:id/button1");
            UiObject but3 = CodeUtils.getUiObject_textOrId("允许", "com.android.packageinstaller:id/permission_allow_button");
            try {
                if (but1.exists()) {
                    showMsg("====点掉版本更新=====");
                    but1.click();
                    break;
                } else if (but2.exists()) {
                    showMsg("=====点掉广告=====");
                    but2.click();
                } else if (but3.exists()) {
                    showMsg("=====允许获取账号信息=====");
                    but3.click();
                }
            } catch (Exception e) {
                showMsg("===查找更新弹窗控件错误===");
                break;
            }
        }
        //判定界面刷新成功点击转账按钮
        showMsg("===点击主页面转账按钮===");
        mDevice.click((int) (270 * ratio), 370);
        sleep(ChatClient.overtime_6);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_90, "登陆前，检测转账超时", Constants.isReport);
            if (isOutTime) {
                return;
            }
            UiObject zhuanzhang0 = CodeUtils.getUiObject_textOrClass("转账", "android.widget.TextView");
            if (zhuanzhang0.exists()) {
                sleep(1000);
                //转账
                showMsg("===登陆前，点击转账===");
                mDevice.click((int) (100 * ratio), (int) (250 * ratio));
                ChatClient.start();//开启与T端的通讯协议
                showMsg("===准备获取账号信息===");
                //获取账号信息
                ChatClient.begintime = System.currentTimeMillis();//开始时间
                while (true) {
                    long nowtime = System.currentTimeMillis();
                    if ((nowtime - ChatClient.begintime) > ChatClient.overtime) {
                        showMsg("====获取账号信息超时===");
                        ChatClient.timer(LogToFile.getMethodName(), ChatClient.errorStr);
                        return;
                    }
                    if (userInfo.equals("")) {
                        ChatClient.msg("<YZF>{'header':'" + ChatClient.M_GET_BANK_LOGIN_INFO_P + "'}</YZF>");
                        sleep(5000);
                    } else {
                        user = new User();
                        String[] res = userInfo.split(" ");
                        showMsg("===用户登陆信息=====");
                        for (int i = 0; i < res.length; i++) {
                            switch (i) {
                                case 0:
                                    user.setUserName(res[0]);
                                    break;
                                case 1:
                                    user.setPwd(res[1]);
                                    break;
                                case 2:
                                    user.setBankpwd(res[2]);
                                    break;
                                case 3:
                                    user.setUsbPayPwd(res[3]);
                                    break;
                                case 4:
                                    user.setUseType(res[4]);
                                    break;
                                case 5:
                                    user.setBankId(res[5]);
                                    break;
                                case 6:
//                                    user.setUsePayMode(res[0].split("_")[1]);
                                    user.setUsePayMode("0");
                                    break;
                            }
                        }
                        break;
                    }
                }
                break;
            } else {
                mDevice.click((int) (270 * ratio), (int) (370 * ratio));
                sleep(ChatClient.overtime_1);
            }
            sleep(ChatClient.overtime_1);
        }
        Login();
    }


    //进入登录页面补充用户信息
    public void Login() {
        CCBMethodName = "Login";
        try {
            //判断是否进入登录页面
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_30, "跳转登录界面超时", Constants.isReport);
                if (isOutTime) {
                    return;
                }
                UiObject login = CodeUtils.getUiObject_textOrClass("登录", "android.widget.Button");
                if (login.exists()) {
                    showMsg("进入登录页...");
                    break;
                }
            }
            UiObject username = CodeUtils.getUiObject_textOrClass("请输入手机号", "android.widget.EditText");
            if (username.exists()) {
                if (user.getUserName() != null) {
                    username.setText(user.getUserName());
                }
            }
            UiObject mET_pwd = CodeUtils.getUiObject_textOrClass("请输入登录密码", "android.widget.EditText");
            if (mET_pwd.exists()) {
                mET_pwd.setText(user.getPwd());
                sleep(2000);
            }

            showMsg("---检测登陆密码是否正确---");
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_5, "检测登陆密码是否正确超时", Constants.notReport);
                if (isOutTime) {
                    break;
                }
                UiObject msg = CodeUtils.getUiObject_textOrId("您的登录密码验证错误，还有5次就将锁定，请您检查后再次输入。", "android:id/message");
                if (msg.exists()) {//应该直接退出并且不会重启
                    showMsg("登陆密码输入错误");
                    ChatClient.timer("登陆", "登陆密码错误");
                    return;
                }
            }
            //登录
            while (true) {
                UiObject mBT_login = CodeUtils.getUiObject_textOrClass("登录", "android.widget.Button");
                if (mBT_login.exists()) {
                    showMsg("登录...");
                    mBT_login.click();
                    break;
                }
            }
            sleep(ChatClient.overtime_2);
            /*******查看是否有弹窗**********/
            UiObject mTV = CodeUtils.getUiObject_textOrClass("暂不开通", "android.widget.Button");
            showMsg("检测暂不开通");
            if (mTV.exists()) {
                mTV.click();
                showMsg("点掉了暂不开通");
            }
            sleep(2000);
            //判定转账界面是否生成
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_60, "检测界面生成超时", Constants.isReport);
                if (isOutTime) {
                    return;
                }
                UiObject mTV1 = CodeUtils.getUiObject_textOrClass("转账", "android.widget.TextView");
                UiObject mTV2 = CodeUtils.getUiObject_textOrId("账号转账", "com.android.bankabc:id/h5_tv_title");
                if (mTV1.exists() || mTV2.exists()) {
                    ChatClient.loginS();
                    break;
                }
                sleep(ChatClient.overtime_1);
            }
            sleep(ChatClient.overtime_3);
            setRemittance();
        } catch (UiObjectNotFoundException e) {
            showMsg("登陆异常信息" + e.toString());
            setDevicesLog(e.getCause().getMessage());
            ChatClient.timer(LogToFile.getMethodName(), ChatClient.errorStr);
            e.printStackTrace();
        }
    }


    /**
     * 登录成功后进入转账
     *
     * @throws UiObjectNotFoundException
     */
    public void setRemittance() {
        try {
            while (true) {
                //判断是否存在订单
                if (ChatClient.existOrder()) {
                    orderBankName = Constants.getBank(list.get(0).getBankName(), "ABC");
                    if (ConfirmOrder) {//T端之前账单待确认
                        showMsg("开始查账");
                        auditOrder();
                    } else {//T端无待确认状态账单直接转账
                        showMsg("开始转账");
                        remittanceLongin();
                    }
                } else {
                    if (PAUSE) {
                        setTimerun();
                    } else {
                        S_paused_C();
                        sleep(ChatClient.overtime_1);
                    }
                }
            }
        } catch (UiObjectNotFoundException e) {
            showMsg("转账异常信息" + e.toString());
            setDevicesLog(e.getCause().getMessage());
            ChatClient.timer(LogToFile.getMethodName(), ChatClient.errorStr);
            e.printStackTrace();
        }
    }


    /**
     * 跨行转账汇款
     */
    public void remittanceLongin() {
        try {
            sleep(ChatClient.overtime_1);
            CCBMethodName = "remittanceLongin";
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_60, "未查到余额View", Constants.isReport);
                if (isOutTime) {
                    return;
                }
                netWorkErr();
                //获取TextView的第7个实例---余额
                UiObject mTV_moeny = mDevice.findObject(new UiSelector().className("android.widget.TextView").instance(7));
                if (mTV_moeny.exists()) {
                    String money = replaceMoney(mTV_moeny.getText());
                    showMsg("获取余额：" + money);
                    sumMoney = Util.replaceMoney(money);
                    showMsg("本地保存余额");
                    String sumMoneyFile = user.getUserName() + BANK_FLAG + "_sumMoney.txt";
                    FileHelper.saveMoneySDFile(false, sumMoney, sumMoneyFile);
                    break;
                }
            }
            //账户余额是否大于转账金额
            if (Double.parseDouble(sumMoney) < Double.parseDouble(list.get(0).getMoney())) {
                ChatClient.reportTransferOrder(BANK_NAME, "账户余额不足", sumMoney, 1);
                //返回上页6
                mDevice.pressBack();
                ChatClient.begintime = System.currentTimeMillis();//开始时间
                while (true) {
                    boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_90, "未反回收款账户页面", Constants.isReport);
                    if (isOutTime) {
                        return;
                    }
                    UiObject zhuanzhang = CodeUtils.getUiObject_textOrClass("收款账户", "android.widget.TextView");
                    if (zhuanzhang.exists()) {
                        sleep(ChatClient.overtime_1);
                        break;
                    }
                }
                //转账
                mDevice.click((int) (100 * ratio), (int) (250 * ratio));
                return;
            }

            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_90, "未进入订单填充界面", Constants.isReport);
                if (isOutTime) {
                    return;
                }
                UiObject bankNo = CodeUtils.getUiObject_textOrClass("请输入账号或手机号", "android.widget.EditText");
                if (bankNo.exists()) {
                    bankNo.click();
                    bankNo.setText(list.get(0).getBankNo());
                    if (!android.os.Build.MODEL.equals("SM-C710F")) {
                        mDevice.click((int) (656 * ratio), (int) (830 * ratio));
                    } else {
                        mDevice.click(980, 1150);
                    }
                    break;
                }
            }
            Log.e("报错点", "remittanceLongin: " + "报错点1");
            sleep(ChatClient.overtime_1);
            //银行名处理
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_90, "转账汇款界面超时", Constants.isReport);
                if (isOutTime) {
                    return;
                }
                /*******开始填充转账表单*********/
                UiObject username = CodeUtils.getUiObject_textOrClass("请输入或选择收款方", "android.widget.EditText");
                if (username.exists()) {
                    username.setText(list.get(0).getUserName());
                    break;
                }
            }
            sleep(ChatClient.overtime_1);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_5, "自动识别银行名", Constants.notReport);
                if (isOutTime) {
                    break;
                }
                UiObject mTV_selectBank = mDevice.findObject(new UiSelector().text("请选择银行").className("android.widget.TextView"));
                if (mTV_selectBank.exists()) {
                    mDevice.click((int) (400 * ratio), (int) (373 * ratio));
                    mDevice.click((int) (400 * ratio), (int) (373 * ratio));
                    sleep(ChatClient.overtime_2);
                    showMsg("开始选择银行");
                    if (selectBank()) {
                        break;
                    } else {
                        return;
                    }
                }
            }
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_40, "订单页面超时1", Constants.isReport);
                if (isOutTime) {
                    return;
                }
                UiObject mET_money = CodeUtils.getUiObject_textOrClass("请输入转账金额", "android.widget.EditText");
                if (mET_money.exists()) {
                    mET_money.setText(list.get(0).getMoney());
                    break;
                }
            }
            Log.e("报错点", "remittanceLongin: " + "报错点4");
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_3, "订单页面超时2", Constants.notReport);
                if (isOutTime) {
                    break;
                }
                UiObject mET_money = CodeUtils.getUiObject_textOrClass("请输入转账金额", "android.widget.EditText");
                if (mET_money.exists()) {
                    mET_money.setText(list.get(0).getMoney());
                    break;
                }
            }

            Log.e("报错点", "remittanceLongin: " + "报错点6");
            mDevice.drag(80, 900, 60, 350, 5);
            //进入下一步
            sleep(ChatClient.overtime_5);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_10, "下一步超时", Constants.notReport);
                if (isOutTime) {
                    showMsg("坐标点击下一步");
                    if (!android.os.Build.MODEL.contains("Lenovo L58041")) {
                        mDevice.click((int) (mBT_next[0] * ratio), (int) (mBT_next[1] * ratioH));
                        sleep(500);
                        mDevice.click((int) (mBT_next[0] * ratio), (int) (mBT_next[1] * ratioH));
                    } else {
                        mDevice.click(540, 1800);
                        sleep(500);
                        mDevice.click(540, 1800);
                    }
                    break;
                }
                UiObject mTV_next = CodeUtils.getUiObject_textOrClass("下一步", "android.widget.Button");
                if (mTV_next.exists()) {
                    showMsg("下一步");
                    sleep(ChatClient.overtime_2);
                    /***************二次匹配下一步按钮点击***********************/
                    mTV_next = mDevice.findObject(new UiSelector().text("下一步").className("android.widget.Button"));
                    if (mTV_next.exists()) {
                        showMsg("点击下一步2");
                        mTV_next.click();
                        break;
                    }
                } else {
                    mDevice.drag(80, 900, 60, 300, 10);
                }
            }
            //账户相同 点击下一步失败弹窗
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_8, "弹框检测超时", Constants.notReport);
                if (isOutTime) {
                    break;
                }
                UiObject but = CodeUtils.getUiObject_textOrId("确定", "android:id/button1");
                String dialog_msage = CodeUtils.getUiObject_text("android.widget.TextView", "android:id/message");
                if (!StringUtils.isEmpty(dialog_msage)) {
                    showMsg("检测到弹框信息----" + dialog_msage);
                    if (isErrorInfo(dialog_msage)) { //检测账户相同的弹框
                        sendUnBankInfo(dialog_msage);
                    }
                    //错误提示弹窗点击返回上一页面,再次进入
                    if (but.exists()) {
                        but.click();
                        sleep(ChatClient.overtime_1);
                        back();
                        sleep(ChatClient.overtime_1);
                        mDevice.click((int) (100 * ratio), (int) (250 * ratio));
                        return;
                    }
                }
            }
            //是否需要确定开户行信息流程
            if (isOverMax(list.get(0).getMoney())) {
                //非工作时间转账
                ChatClient.begintime = System.currentTimeMillis();//开始时间
                while (true) {
                    sleep(ChatClient.overtime_2);
                    boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_10, "非工作时间转账弹窗检测超时", Constants.notReport);
                    if (isOutTime) {
                        break;
                    }
                    UiObject maxDialog = CodeUtils.getUiObject_textOrId
                            ("您的转账超过了此时段实时处理的金额限制（不超过5万），本次转账将转人工处理。处理前不会扣款，请保持账户余额充足，并避免重复提交。人工处理时间一般为一个工作日，以最终处理时间为准。您确定提交吗？",
                                    "android:id/message");
                    UiObject sure = mDevice.findObject(new UiSelector().resourceId("android:id/button1").className("android.widget.Button"));
                    showMsg("查找非工作时间转账弹窗");
                    if (maxDialog.exists()) {
                        info = "非工作时间段大额预约处理," + maxDialog.getText();
                        ChatClient.reportTransferOrder(BANK_NAME, info, sumMoney, 6);
                        sure.click();
                        break;
                    }
                }
                //得到开户行信息
                showMsg("开户行信息");
                String[] array = list.get(0).getBankAdr().split("-");
                List<String> bankInfo = new ArrayList<>();
                String s = "";
                for (int i = 0; i < array.length; i++) {
                    if (i < 2) {
                        bankInfo.add(array[i]);
                    } else {
                        if (array.length == 3) {
                            bankInfo.add(array[i]);
                        } else {
                            showMsg("开户行截取超过3");
                            if (i == 2) {
                                s += array[i];
                            } else {
                                s += "-" + array[i];
                            }
                            showMsg("拼接开户行后面的字段");
                            if (i == array.length - 1) {
                                showMsg("拼接完成");
                                bankInfo.add(s);
                            }
                        }
                    }
                }
                sleep(ChatClient.overtime_2);
                showMsg("===准备进入选择省份====");
                setBankInfo(bankInfo);//大于5W会弹出确定开户行信息
            } else {//开始转账验证
                if (user.getUsePayMode().equals("0")) {//U盾认证
                    UNextStep();
                } else {//密码验证
                    pwdNextStep();
                }
                getUserInfo(sumMoney);
            }

        } catch (UiObjectNotFoundException e) {
            setDevicesLog(e.getMessage());
            ChatClient.timer(LogToFile.getMethodName(), ChatClient.errorStr);
            e.printStackTrace();
        }
    }


    /**
     * 银行名没有自动识别，手动去搜索
     *
     * @return 是否成功搜索选择到银行名
     * @throws UiObjectNotFoundException
     */
    private boolean selectBank() throws UiObjectNotFoundException {
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_15, "进入银行名选择超时", Constants.notReport);
            if (isOutTime) {
                mDevice.click((int) (400 * ratio), (int) (373 * ratio));
                sleep(1000);
                ChatClient.begintime = System.currentTimeMillis();//开始时间
            }
            //在搜索栏搜索所需银行
            UiObject click = CodeUtils.getUiObject_textOrId("搜索", "com.android.bankabc:id/edt_search");
            if (click.exists()) {
                click.setText(orderBankName);
                break;
            }
        }
        sleep(2000);
        Log.e("报错点", "remittanceLongin: " + "报错点2");
        //选择银行
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_15, "银行名错误", Constants.notReport);
            if (isOutTime) {
                ChatClient.reportTransferOrder(BANK_NAME, "银行名错误", sumMoney, 1);
                //返回上页
                mDevice.pressBack();
                sleep(ChatClient.overtime_3);
                mDevice.pressBack();
                sleep(ChatClient.overtime_3);
                clickTransfer();
                return false;
            }
            //搜索出的银行条目
            UiObject click0 = mDevice.findObject(new UiSelector().text(orderBankName).resourceId("com.android.bankabc:id/tv_title"));
            if (click0.exists()) {
                click0.click();
                return true;
            }
        }
    }

    /**
     * 进入转账列表页
     *
     * @throws UiObjectNotFoundException
     */
    private void clickTransfer() {
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_90, "返回《转账》失败", Constants.isReport);
            if (isOutTime) {
                return;
            }
            UiObject zhuanzhang = CodeUtils.getUiObject_textOrClass("转账记录", "android.widget.TextView");
            if (zhuanzhang.exists()) {
                sleep(1000);
                break;
            }
        }
        //转账
        showMsg("===进入转账列表页====");
        mDevice.click((int) (100 * ratio), (int) (250 * ratio));
        mDevice.click((int) (100 * ratio), (int) (250 * ratio));
        sleep(ChatClient.overtime_3);
    }

    /**
     * 通过U盾进行转账
     */
    private void UNextStep() throws UiObjectNotFoundException {
        CCBMethodName = "Submit";
        //确认转账
        setNext();
        addWorkTime(ChatClient.overtime_90);
        sleep(ChatClient.overtime_3);
        boolean isNotPaw = false;//是否是免密登录
        while (true) {
            UiObject editText = mDevice.findObject(new UiSelector().text("8位密码").className("android.widget.EditText"));
            UiObject editText1 = mDevice.findObject(new UiSelector().text("请输入8位密码").className("com.ronganchina.rakeyboard" +
                    ".RAPasswodEdit"));
            UiObject editLable = mDevice.findObject(new UiSelector().textContains("请输入K宝密码").className("android.widget.TextView"));
            showMsg("----检测U盾输入密码框----");

            if (editText.exists() || editLable.exists()) {
                if (editText.exists()) {
                    editText.click();
                } else {
                    mDevice.click((int) (380 * ratio), (int) (520 * ratioH));
                }
                isNewU = false;
                showMsg("===点击密码输入框1===");
                break;
            } else if (editText1.exists()) {
                if (editText1.exists()) {
                    editText1.click();
                } else {
                    mDevice.click((int) (380 * ratio), (int) (240 * ratio));
                }
                isNewU = true;
                showMsg("===点击密码输入框2===");
                break;
            }
            showMsg("----检测U盾提示信息----");
            UiObject upwd2 = CodeUtils.getUiObject_containTextOrClass("K宝屏显翻转", "android.widget.TextView");
            UiObject upwd3 = CodeUtils.getUiObject_textOrClass("请核对K宝显示的内容是否正确，如确认请按“OK”键（或“√”键），取消请按“C”键（或“×”键）", "android.widget.TextView");
            UiObject upwd5_1 = CodeUtils.getUiObject_textOrClass("请核对K宝显示内容，如确认请按K宝的[OK]键，取消请按K宝的[C]键。", "android.widget.TextView");
            if (!upwd5_1.exists()) {
                upwd5_1 = mDevice.findObject(new UiSelector().textContains("请核对K宝显示内容").className("android.widget.TextView"));
            }
            if (upwd2.exists() || upwd3.exists() || upwd5_1.exists()) {
                showMsg("===免密码转账===");
                isNotPaw = true;
                break;
            }
            showMsg("----检测确认转账----");
            UiObject ok = mDevice.findObject(new UiSelector().text("确定").className("android.widget.Button"));
            UiObject ok1 = mDevice.findObject(new UiSelector().text("确认转账").className("android.widget.Button"));
            UiObject zhuanzhang = CodeUtils.getUiObject_textOrClass("继续转账", "android.widget.Button");
            if (ok1.exists()) {
                showMsg("----未检测U盾输入密码框----");
                ok1.click();
                addWorkTime(20000);
                sleep(1000);
                showMsg("===未检测到U盾再来===");
                setNext();
            } else if (ok.exists()) {
                ok.click();
                showMsg("===点击确认===");
            } else if (zhuanzhang.exists()) {
                showMsg("===发现继续转账===");
                break;
            }
        }
        CCBMethodName = "Re_send";
        //U盾密码输入
        if (!isNotPaw) {
            setUpwd();
            showMsg("---检测是否U盾密码输入正确----");
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_5, "检测是否U盾密码输入正确超时", Constants.notReport);
                if (isOutTime) {
                    break;
                }
                UiObject u_pwd = CodeUtils.getUiObject_textOrClass("K宝密码错误，您还有5次重试机会", "android.widget.TextView");
                if (u_pwd.exists()) {
                    showMsg("---U盾输入密码错误----");
                    sendUErrorInfo("U盾密码输入错误");
                    return;
                }
            }
        } else {
            showMsg("---免密点击U盾----");
            sleep(ChatClient.overtime_3);
            pressu("2");
            isNotPaw = false;
        }
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_40, "结果页超时", Constants.isReport);
            if (isOutTime) {
                return;
            }
            UiObject zhuanzhang = CodeUtils.getUiObject_textOrClass("继续转账", "android.widget.Button");
            UiObject ok = CodeUtils.getUiObject_textOrClass("确定", "android.widget.Button");
            UiObject back = CodeUtils.getUiObject_textOrClass("返回", "android.widget.Button");

            if (zhuanzhang.exists()) {
                sleep(1000);
                break;
            } else if (ok.exists()) {//todo
                sleep(1000);
                ok.click();
                addWorkTime(20000);
                sleep(1000);
                showMsg("===未检测到U盾===");
                setNext();
                sleep(1000);
                setUpwd();
                ChatClient.begintime = System.currentTimeMillis();//开始时间
            } else if (back.exists()) {
                back.click();
                sleep(1000);
                sendFailInfo("交易失败");
                return;
            }
        }
        Log.e("报错点", "remittanceLongin: " + "报错点10");
    }

    /**
     * 密码验证
     */
    private void pwdNextStep() {
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_40, "密码输入框超时", Constants.isReport);
            if (isOutTime) {
                return;
            }
            UiObject mV_pwd_title = CodeUtils.getUiObject_textOrId("请输入账户密码", "div_pwd_title");
            if (mV_pwd_title.exists()) {
                showMsg("进入密码验证...");
                break;
            }
        }
        List<PointBean> pwd_key_xy = get_pwd_Key_XY(BANK_FLAG);
        for (char c : user.getPwd().toCharArray()) {
            for (PointBean point_xy : pwd_key_xy) {
                if (point_xy.getCharStr().equals(c)) {
                    showMsg("点击密码----+1");
                    mDevice.click(point_xy.getX(), point_xy.getY());
                    sleep(ChatClient.overtime_1);
                    break;
                }
            }
        }
    }


    /**
     * 密码转账键盘截图
     */
    public List<PointBean> get_pwd_Key_XY(String bankname) {
        List<PointBean> pointBeanList;
        Bitmap bitmap = null;
        if (screenHeight == 2016) {
            showMsg("截图新U盾键盘");
            bitmap = ImageView.getImage1(0, 1368, 1080, 2016, 1080, 648);
        } else if (screenHeight == 1920) {

        } else {

        }

        pointBeanList = saveImg22(bitmap, bankname);
        return pointBeanList;
    }


    /**
     * 发送U盾错误信息
     **/
    private void sendUErrorInfo(String s) {
        sendUnBankInfo(s);
        sleep(ChatClient.overtime_1);
        ChatClient.timer("reason", s);
    }

    /**
     * 发送错误信息
     **/
    private void sendFailInfo(String s) {
        sendUnBankInfo(s);
        sleep(ChatClient.overtime_5);
        //转账
        mDevice.click((int) (270 * ratio), (int) (370 * ratio));
        mDevice.click((int) (270 * ratio), (int) (370 * ratio));
        sleep(ChatClient.overtime_3);
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_60, "检测收款账户超时", Constants.isReport);
            if (isOutTime) {
                return;
            }
            UiObject zhuanzhang = CodeUtils.getUiObject_textOrClass("收款账户", "android.widget.TextView");
            showMsg("--检测收款账户--");
            if (zhuanzhang.exists()) {
                sleep(ChatClient.overtime_1);
                break;
            }
        }
        mDevice.click((int) (100 * ratio), (int) (250 * ratio));
        mDevice.click((int) (100 * ratio), (int) (250 * ratio));
        showMsg("--再次转账--");
    }

    /**
     * 设置开户行信息
     *
     * @param array
     */
    private void setBankInfo(List<String> array) throws UiObjectNotFoundException {
        addWorkTime(ChatClient.overtime_30);
        //检测开户行弹窗
        checkCity(array);
        if (!isKHHDialog) {
            //汇款的后续流程
            UNextStep();
        }
    }

    /**
     * 检测是否有开户行弹窗
     *
     * @param array
     */
    private void checkCity(List<String> array) throws UiObjectNotFoundException {
        showMsg("检测是否有开户行弹窗");
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_8, "检测是否弹出开户行超时", false);
            if (isOutTime) {
                isKHHDialog = false;
                return;
            }
            UiObject text = mDevice.findObject(new UiSelector().text("请尽量下拉选择开户网点，否则交易将无法实时受理").className("android.widget.TextView"));
            showMsg("检测是否弹出开户行");
            //设置开户省
            if (text.exists()) {
                UiObject frameLayout, layout1, view, view1, view6, view2;
                frameLayout = mDevice.findObject(new UiSelector().className("android.widget.FrameLayout").index(0));
                layout1 = frameLayout.getChild(new UiSelector().className("android.widget.LinearLayout").index(0));
                view = layout1.getChild(new UiSelector().className("android.view.ViewGroup").index(1));
                view1 = view.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
                view6 = view1.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
                view2 = view6.getChild(new UiSelector().className("android.view.ViewGroup").index(2));
                showMsg("检测开户省");
                if (view2.exists()) {
                    view2.click();
                    showMsg("找到开户省");
                    break;
                }
            }
            sleep(ChatClient.overtime_2);
        }
        sleep(ChatClient.overtime_2);
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            sleep(ChatClient.overtime_2);
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_20, "检测搜索到开户省超时", false);
            if (isOutTime) {
                break;
            }
            UiObject etSearch = mDevice.findObject(new UiSelector().text("搜索").resourceId("com.android.bankabc:id/edt_search"));
            showMsg("是否搜索到开户省");
            if (etSearch.exists()) {
                etSearch.setText(array.get(0));
                showMsg("设置开户省----" + array.get(0));
                break;
            } else {
                showMsg("未找到搜索框----" + array.get(0));
            }
        }
        sleep(ChatClient.overtime_2);
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            sleep(ChatClient.overtime_2);
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_20, "检测是否搜索到开户省超时", false);
            if (isOutTime) {
                break;
            }
            UiObject list = mDevice.findObject(new UiSelector().className("android.widget.ListView").resourceId("com.android" +
                    ".bankabc:id/lv_title_list"));
            showMsg("是否搜索到开户省");
            if (list.exists()) {
                UiObject item = mDevice.findObject(new UiSelector().className("android.widget.RelativeLayout").resourceId("com.android" +
                        ".bankabc:id/layout_item"));
                if (item.exists()) {
                    showMsg("搜索到开户省");
                    item.click();
                    break;
                } else {
                    showMsg("未找到开户省");
                }
            } else {
                showMsg("未找到开户省的list");
            }
        }
        sleep(ChatClient.overtime_2);
        ChatClient.begintime = System.currentTimeMillis();
        //设置开户城市
        while (true) {
            sleep(ChatClient.overtime_2);
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_20, "检测检测开户城市超时", false);
            if (isOutTime) {
                break;
            }
            UiObject text = mDevice.findObject(new UiSelector().text("请尽量下拉选择开户网点，否则交易将无法实时受理").className("android.widget.TextView"));
            if (text.exists()) {
                UiObject view, view1, view6, view2, frameLayout, layout1;
                frameLayout = mDevice.findObject(new UiSelector().className("android.widget.FrameLayout").index(0));
                layout1 = frameLayout.getChild(new UiSelector().className("android.widget.LinearLayout").index(0));
                view = layout1.getChild(new UiSelector().className("android.view.ViewGroup").index(1));
                view1 = view.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
                view6 = view1.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
                view2 = view6.getChild(new UiSelector().className("android.view.ViewGroup").index(4));
                showMsg("检测开户城市");
                if (view2.exists()) {
                    showMsg("找到开户城市");
                    view2.click();
                    break;
                }
            }
        }
        sleep(ChatClient.overtime_2);
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            sleep(ChatClient.overtime_2);
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_20, "检测是否搜索到开户城市超时", false);
            if (isOutTime) {
                break;
            }
            UiObject etSearch = mDevice.findObject(new UiSelector().text("搜索").resourceId("com.android.bankabc:id/edt_search"));
            showMsg("是否搜索到开户城市");
            if (etSearch.exists()) {
                etSearch.setText(array.get(1));
                showMsg("设置开户城市");
                break;
            }
        }
        sleep(ChatClient.overtime_2);
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            sleep(ChatClient.overtime_2);
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_20, "检测是否搜索到开户省超时", false);
            if (isOutTime) {
                break;
            }
            UiObject list = mDevice.findObject(new UiSelector().className("android.widget.ListView").resourceId("com.android" +
                    ".bankabc:id/lv_title_list"));
            showMsg("是否搜索到开户省");
            if (list.exists()) {
                UiObject item = mDevice.findObject(new UiSelector().className("android.widget.RelativeLayout").resourceId("com" +
                        ".android.bankabc:id/layout_item"));
                if (item.exists()) {
                    showMsg("搜索到开户省");
                    item.click();
                    break;
                }
            }
        }
        sleep(ChatClient.overtime_2);
        ChatClient.begintime = System.currentTimeMillis();
        //设置开户网点
        while (true) {
            sleep(ChatClient.overtime_2);
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_20, "检测检测开户网点超时", false);
            if (isOutTime) {
                break;
            }
            UiObject text = mDevice.findObject(new UiSelector().text("请尽量下拉选择开户网点，否则交易将无法实时受理").className("android.widget.TextView"));
            if (text.exists()) {
                UiObject view, view1, view6, view2, layout1, frameLayout;
                frameLayout = mDevice.findObject(new UiSelector().className("android.widget.FrameLayout").index(0));
                layout1 = frameLayout.getChild(new UiSelector().className("android.widget.LinearLayout").index(0));
                view = layout1.getChild(new UiSelector().className("android.view.ViewGroup").index(1));
                view1 = view.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
                view6 = view1.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
                view2 = view6.getChild(new UiSelector().className("android.view.ViewGroup").index(6));
                showMsg("检测开户网点");
                if (view2.exists()) {
                    view2.click();
                    showMsg("点击选择开户网点");
                    break;
                }
            }
        }
        sleep(ChatClient.overtime_2);
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            sleep(ChatClient.overtime_2);
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_20, "检测搜索开户网点超时", false);
            if (isOutTime) {
                break;
            }
            UiObject etSearch = mDevice.findObject(new UiSelector().text("搜索").resourceId("com.android.bankabc:id/edt_search"));
            showMsg("搜索开户网点");
            if (etSearch.exists()) {
                etSearch.setText(array.get(2));
                break;
            }
        }
        sleep(ChatClient.overtime_2);
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            sleep(ChatClient.overtime_2);
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_20, "检测是否搜索到开户网点超时", false);
            if (isOutTime) {
                break;
            }
            UiObject listView = mDevice.findObject(new UiSelector().className("android.widget.ListView").resourceId("com.android" +
                    ".bankabc:id/lv_title_list"));
            showMsg("是否搜索到开户网点");
            if (listView.exists()) {
                UiObject item = mDevice.findObject(new UiSelector().className("android.widget.RelativeLayout").resourceId("com.android" +
                        ".bankabc:id/layout_item"));
                if (item.exists()) {
                    showMsg("搜索到开户网点");
                    item.click();
                    break;
                } else {
                    showMsg("未搜索到开户网点");
                    UiObject back = mDevice.findObject(new UiSelector().className("android.widget.ImageView").resourceId("com.android" +
                            ".bankabc:id/iv_back"));
                    if (back.exists()) {
                        back.click();
                        showMsg("点击返回");
                        sendUnBankInfo("未搜索到开户网点");
                        showMsg("上报未搜索到开户网点");
                        jumpOut();
                        return;
                    }
                }
            }
        }
        sleep(ChatClient.overtime_2);
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_10, "检测开户行确认超时", false);
            if (isOutTime) {
                return;
            }
            sleep(ChatClient.overtime_2);
            UiObject sure = mDevice.findObject(new UiSelector().text("确认").className("android.widget.Button"));
            showMsg("检测开户行确认");
            if (sure.exists()) {
                showMsg("点击开户行确认");
                sure.click();
                break;
            }
        }
        showMsg("进入转账的后续流程");
        UNextStep();
    }

    /**
     * 返回
     */
    private void jumpOut() throws UiObjectNotFoundException {
        sleep(ChatClient.overtime_2);
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_30, "查找关闭按钮超时", false);
            if (isOutTime) {
                break;
            }
            UiObject view, view1, view6, view8, view9, view10, view11, layout1;
            layout1 = mDevice.findObject(new UiSelector().className("android.widget.LinearLayout").index(0));
            view = layout1.getChild(new UiSelector().className("android.view.ViewGroup").index(1));
            view1 = view.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
            view6 = view1.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
            view8 = view6.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
            view9 = view8.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
            view10 = view9.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
            view11 = view10.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
            showMsg("查找关闭按钮");
            if (view11.exists()) {
                showMsg("点击关闭按钮");
                view11.click();
                break;
            }
        }
        sleep(ChatClient.overtime_2);
        while (true) {
            UiObject scroll = mDevice.findObject(new UiSelector().className("android.widget.ScrollView").index(0));
            UiObject zzView, zzView1, zzView2;
            zzView = scroll.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
            zzView1 = zzView.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
            zzView2 = zzView1.getChild(new UiSelector().className("android.view.ViewGroup").index(0));
            UiObject backBtn = zzView2.getChild(new UiSelector().className("android.widget.Button").index(0));
            showMsg("查找转账页关闭按钮");
            if (backBtn.exists()) {
                showMsg("点击转账页关闭按钮");
                backBtn.click();
                break;
            }
        }
        if (list.size() != 0) {
            //删除处理过的订单
            mapDelete(list.get(0).getOrder());
            getmap();
            showMsg("删除订单");
        }
        sleep(ChatClient.overtime_5);
        //转账
        mDevice.click((int) (350 * ratio), (int) (250 * ratio));
        mDevice.click((int) (350 * ratio), (int) (250 * ratio));
        showMsg("准备再次接单");
    }

    private void back() {
        mDevice.click(100, 145);
    }

    /**
     * 判断是否达到选择开户行的临界值
     *
     * @path sumMoney
     */
    private boolean isOverMax(String s) {
        boolean b = false;
        double c = 50000.00;//临界值为50000.00
        showMsg("===判断金额是否超过填写开户行的临界值====");
        double money = Double.parseDouble(s);
        if (money > c) {
            b = true;
        }
        showMsg("===判断金额是否超过填写开户行的临界值====" + b);
        return b;
    }

    /**
     * 完成转账后回复服务端
     *
     * @param sumMoney：余额
     */
    public void getUserInfo(String sumMoney) {
        String type = "";
        String errorStr = "";
        sleep(ChatClient.overtime_1);
        ChatClient.msg("<YZF>{'header':'" + ChatClient.M_TRANSFERFINISH_P + "'}</YZF>");
        try {
            if (isOverMax(list.get(0).getMoney())) {
                if (info.contains("非工作时间段大额预约处理")) {
                    type = "非工作时间段大额预约处理";
                    errorStr = info;
                }
            } else {
                type = "转账成功";
                errorStr = "转账成功";
            }
            String balance = "";
            if (Constants.getStr(type) == 0) {
                DecimalFormat formater = new DecimalFormat("#0.##");
                balance = formater.format(Double.parseDouble(sumMoney) - Double.parseDouble(list.get(0).getMoney()));
                String sumMoneyFile = user.getUserName() + BANK_NAME + "_sumMoney.txt";
                FileHelper.saveMoneySDFile(false, balance, sumMoneyFile);
            }
            errorStr = errorStr.replaceAll("\r|\n|\\s", "");
            ChatClient.reportTransferOrder(BANK_NAME, errorStr, balance, Constants.getStr(type));
            sleep(ChatClient.overtime_3);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_40, "未查到《完成》", Constants.isReport);
                if (isOutTime) {
                    return;
                }
                UiObject click = CodeUtils.getUiObject_textOrClass("完成", "android.widget.Button");
                if (click.exists()) {
                    click.click();
                    break;
                }
            }
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_60, "返回《转账页》超时", Constants.isReport);
                if (isOutTime) {
                    return;
                }
                UiObject zhuanzhang = CodeUtils.getUiObject_textOrClass("收款账户", "android.widget.TextView");
                if (zhuanzhang.exists()) {
                    sleep(ChatClient.overtime_1);
                    break;
                }
            }
            //转账
            mDevice.click((int) (100 * ratio), (int) (250 * ratio));
            mDevice.click((int) (100 * ratio), (int) (250 * ratio));
        } catch (UiObjectNotFoundException e) {
            setDevicesLog(e.getCause().getMessage());
            ChatClient.timer(LogToFile.getMethodName(), ChatClient.errorStr);
            e.printStackTrace();
        }
    }


    /**
     * 查账
     *
     * @throws UiObjectNotFoundException
     */
    public void auditOrder() throws UiObjectNotFoundException {
        addWorkTime(ChatClient.overtime_40);
        getBalance();
        sleep(ChatClient.overtime_1);
        mDevice.pressBack();
        sleep(ChatClient.overtime_2);
        while (true) {
            UiObject mTV_orderList = CodeUtils.getUiObject_textOrClass("转账记录", "android.widget.TextView");
            if (mTV_orderList.exists()) {
                showMsg("点击转账记录");
                mTV_orderList.click();
                sleep(ChatClient.overtime_2);
                break;
            }
        }
        String name_bankNo = "";
        String money = "";
        ChatClient.begintime = System.currentTimeMillis();
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_30, "查询超时...", Constants.notReport);
            if (isOutTime) {
                getOrderTimeOut();
                return;
            }
            UiObject mSV = mDevice.findObject(new UiSelector().className("android.widget.ScrollView").index(0));
            UiObject mLL1 = mSV.getChild(new UiSelector().className("android.widget.LinearLayout").index(0));
            UiObject mLL2 = mLL1.getChild(new UiSelector().className("android.widget.LinearLayout").index(0));
            UiObject mTV_name_bankNo = mLL2.getChild(new UiSelector().className("android.widget.TextView").index(1));
            UiObject mTV_money = mLL2.getChild(new UiSelector().className("android.widget.TextView").index(2));
            if (mTV_name_bankNo.exists()) {
                name_bankNo = mTV_name_bankNo.getText();
                if (StringUtils.isEmpty(name_bankNo)) {
                    name_bankNo = mTV_name_bankNo.getContentDescription();
                }
            }
            if (mTV_money.exists()) {
                money = mTV_money.getText();
                if (StringUtils.isEmpty(money)) {
                    money = mTV_money.getContentDescription();
                }
            }
            if (!TextUtils.isEmpty(money)) {
                money = money.trim().replaceAll("-", "");
                money = money.trim().replaceAll(" ", "");
                money = money.trim().replaceAll(",", "");
            }
            if (!StringUtils.isEmpty(name_bankNo) && !StringUtils.isEmpty(money)) {
                showMsg("查到订单：银行卡号：" + name_bankNo + "金额：" + money);
                break;
            }
        }

        //比对数据
        if (!StringUtils.isEmpty(name_bankNo)) {
            String bankNo0 = list.get(0).getBankNo().substring(list.get(0).getBankNo().length() - 4, list.get(0).getBankNo().length());
            String bankNo = name_bankNo.substring(name_bankNo.length() - 5, name_bankNo.length() - 1);
            String state = "1";
            if (name_bankNo.contains(list.get(0).getUserName()) && Double.parseDouble(list.get(0).getMoney()) == Double.parseDouble(money) &&
                    bankNo0.equals(bankNo)) {
                state = "0";
            } else {
                String sumMoneyFile = user.getUserName() + BANK_FLAG + "_sumMoney.txt";
                String oldMoney = FileHelper.readSDFile(sumMoneyFile);
                state = Constants.changeInAmount(BANK_FLAG, oldMoney, sumMoney, list.get(0).getMoney());
            }
            ChatClient.reportAuditOrder(state, this);
            sleep(ChatClient.overtime_1);
        }
        //返回
        mDevice.pressBack();
        sleep(ChatClient.overtime_2);
        while (true) {
            UiObject zhuanzhang = CodeUtils.getUiObject_textOrClass("转账", "android.widget.TextView");
            if (zhuanzhang.exists()) {
                //转账
                showMsg("点击进入转账表单");
                mDevice.click((int) (100 * ratio), (int) (250 * ratio));
                mDevice.click((int) (100 * ratio), (int) (250 * ratio));
                sleep(ChatClient.overtime_1);
                break;
            }
        }
    }

    /**
     * 查账订单超时返回
     */
    private void getOrderTimeOut() {
        ChatClient.reportAuditOrder("6", this);
        //返回
        mDevice.pressBack();
        sleep(ChatClient.overtime_2);
        while (true) {
            UiObject zhuanzhang = CodeUtils.getUiObject_textOrClass("转账", "android.widget.TextView");
            if (zhuanzhang.exists()) {
                //转账
                showMsg("点击进入转账表单");
                mDevice.click((int) (100 * ratio), (int) (250 * ratio));
                mDevice.click((int) (100 * ratio), (int) (250 * ratio));
                sleep(ChatClient.overtime_1);
                break;
            }
        }
    }

    /**
     * 获取余额
     */
    private void getBalance() throws UiObjectNotFoundException {
        sleep(ChatClient.overtime_2);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_40, "转账界面获取余额超时", Constants.isReport);
            if (isOutTime) {
                return;
            }
            UiObject username = CodeUtils.getUiObject_textOrClass("请输入或选择收款方", "android.widget.EditText");
            if (username.exists()) {
                //获取余额
                UiObject moeny = mDevice.findObject(new UiSelector().className("android.widget.TextView").instance(7));
                sumMoney = replaceMoney(moeny.getText());
                showMsg("money:" + sumMoney);
                //上报余额
                C_updateBalance_W(Double.parseDouble(sumMoney));
                break;
            }
        }
    }

    /**
     * 等待订单
     *
     * @throws UiObjectNotFoundException
     */
    public void setTimerun() {
        try {
            showMsg("等待订单");
            CCBMethodName = "setTimerun";
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_30, "等待订单，转账界面超时", Constants.notReport);
                if (isOutTime) {
                    if (!netWorkErr()) {
                        ChatClient.timer(LogToFile.getMethodName(), ChatClient.errorStr);
                        return;
                    }
                }
                UiObject username = CodeUtils.getUiObject_textOrClass("请输入或选择收款方", "android.widget.EditText");
                if (username.exists()) {
                    //获取余额
                    UiObject mTV_moeny = mDevice.findObject(new UiSelector().className("android.widget.TextView").instance(7));
                    if (mTV_moeny.exists()) {
                        String money = replaceMoney(mTV_moeny.getText());
                        showMsg("获取余额：" + money);
                        sumMoney = Util.replaceMoney(money);
                        C_updateBalance_W(Double.parseDouble(sumMoney));
                        showMsg("本地保存余额");
                        String sumMoneyFile = user.getUserName() + BANK_FLAG + "_sumMoney.txt";
                        FileHelper.saveMoneySDFile(false, sumMoney, sumMoneyFile);
                        break;
                    }
                }
                netWorkErr();
            }

            if (!ChatClient.waitExistOrder(ChatClient.overtime_60)) {
                mDevice.click((int) (410 * ratio), (int) (480 * ratio));
                Constants.CODE = "";
                ChatClient.begintime = System.currentTimeMillis();//开始时间
                while (true) {
                    boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_60, "未找到《确定》View", Constants.isReport);
                    if (isOutTime) {
                        return;
                    }
                    UiObject zhuanzhang = CodeUtils.getUiObject_textOrId("确定", "android:id/button1");
                    if (zhuanzhang.exists()) {
                        zhuanzhang.click();
                        break;
                    }
                }
            }
        } catch (Exception e) {
            setDevicesLog(e.getCause().getMessage());
            ChatClient.timer("setTimerun", ChatClient.error);
            e.printStackTrace();
        }
    }

    /**
     * 处理网络不稳定弹窗
     */
    private boolean netWorkErr() throws UiObjectNotFoundException {
        UiObject mDialog_message = mDevice.findObject(new UiSelector().resourceId("android:id/message").textContains("网络不稳定"));
        if (mDialog_message.exists()) {
            UiObject mBT_Dialog = mDevice.findObject(new UiSelector().text("确定").resourceId("android:id/button1"));
            if (mBT_Dialog.exists()) {
                showMsg("出现网络不稳定...");
                mBT_Dialog.click();
                sleep(ChatClient.overtime_1);
                return true;
            }
        }
        return false;
    }

    /**
     * 得到可用金额
     *
     * @param str
     * @return
     */
    public String replaceMoney(String str) {
        String money = "";
        String regxp = "((-)?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){1,2})?)";
        str = str.replaceAll(",", "");
        Pattern p = Pattern.compile(regxp);
        Matcher m = p.matcher(str);
        while (m.find()) {
            money = m.group();
        }
        return StringUtils.isEmpty(money) ? "0.00" : money;
    }

    // U盾截去数字键盘
    public void getNum_U_all(String pwd) {
        List<PointBean> pointBeanList;
        try {
            Bitmap bitmap = null;
            if (screenHeight == 2102) {
                bitmap = ImageView.getImage1(0, (int) (1080 * ratio), (int) (720 * ratio), (int) (1384 * ratio), (int) (720 *
                        ratio), (int) (304 * ratio));
            } else if (screenHeight == 2016) {
                bitmap = ImageView.getImage1(0, (int) (982 * ratio), (int) (720 * ratio), (int) (1344 * ratio), (int) (720 *
                        ratio), (int) (362 * ratio));
            } else if (screenHeight == 1920) {
                bitmap = ImageView.getImage1(0, (int) (990 * ratio), (int) (720 * ratio), (int) (1274 * ratio), (int)
                        (720 * ratio), (int) (189 * ratio));
            } else {
                bitmap = ImageView.getImage1(0, (int) (840 * ratio), (int) (720 * ratio), (int) (1280 * ratio), (int) (720 *
                        ratio), (int) (440 * ratio));
            }
            pointBeanList = saveImg22(bitmap, BANK_FLAG);
            showMsg("pointBeanList=" + pointBeanList.size());
            setClick_U_535(pointBeanList, pwd);
        } catch (Exception e) {
            setDevicesLog(e.getCause().getMessage());
            e.printStackTrace();
        }
    }


    /**
     * 截去英文键盘模拟用户输入密码
     * U盾密码字母键盘
     *
     * @param pwd:要点击的密码
     * @throws UiObjectNotFoundException
     */
    public void getABC_U_all(String pwd) {
        List<PointBean> pointBeanList;
        try {
            Bitmap bitmap = null;
            if (screenHeight == 2102) {
                bitmap = ImageView.getImage1(0, (int) (1080 * ratio), (int) (720 * ratio), (int) (1384 * ratio), (int) (720 *
                        ratio), (int) (304 * ratio));
            } else if (screenHeight == 2016) {
                bitmap = ImageView.getImage1(0, (int) (982 * ratio), (int) (720 * ratio), (int) (1344 * ratio), (int) (720 *
                        ratio), (int) (362 * ratio));
            } else if (screenHeight == 1920) {
                bitmap = ImageView.getImage1(0, (int) (990 * ratio), (int) (720 * ratio), (int) (1274 * ratio), (int) (720 *
                        ratio), (int) (189 * ratio));
            } else {
                bitmap = ImageView.getImage1(0, (int) (840 * ratio), (int) (720 * ratio), (int) (1280 * ratio), (int) (720 *
                        ratio), (int) (440 * ratio));
            }
            pointBeanList = saveImg22(bitmap, BANK_FLAG);
            setClick_U_535(pointBeanList, pwd);
        } catch (
                Exception e) {
            setDevicesLog(e.getCause().getMessage());
            e.printStackTrace();
        }

    }

    /**
     * 模拟点击密码
     *
     * @param pad_ 要点击的
     */
    public void setClick_U_535(List<PointBean> pointBeanList, String pad_) {
        String tempStr = "";
        showMsg("pointBeanList--------->" + pointBeanList.size());
        if (pointBeanList.size() > 0) {
            for (int i = 0; i < pointBeanList.size(); i++) {
                tempStr += pointBeanList.get(i).getCharStr();
            }
        }
        showMsg("pointBeanList--------->" + pointBeanList.size());
        char[] abc_num = tempStr.toCharArray();//字母或者数字数组
        char[] pwd = pad_.toCharArray();//要点击的密码char数组
        showMsg("tempStr==" + tempStr);
        showMsg("pad_==" + pad_.length());
        for (int j = 0; j < pwd.length; j++) {
            for (int i = 0; i < abc_num.length; i++) {
                if (abc_num[i] == pwd[j]) {
                    if (isNumeric(String.valueOf(pwd[j]))) {
                        sleep(1000);
                        //点击数字
                        int num = pointBeanList.get(i).getY();
                        showMsg("Y轴坐标为：-------" + num);
                        if (screenHeight == 2102) {
                            mDevice.click((pointBeanList.get(i).getX()), (int) (num + 1090 * ratio));
                        } else if (screenHeight == 2016) {
                            mDevice.click((pointBeanList.get(i).getX()), (int) (num + 993 * ratio));
                        } else if (screenHeight == 1920) {
                            mDevice.click((pointBeanList.get(i).getX()), (int) (num + 1002 * ratio));
                        } else {
                            mDevice.click((pointBeanList.get(i).getX()), (int) (num + 840 * ratio));
                        }
                        showMsg("点击第" + j + "位");
                    } else {
                        sleep(1000);
                        //点击字母
                        int num = pointBeanList.get(i).getY();
                        if (screenHeight == 2102) {
                            mDevice.click((pointBeanList.get(i).getX()), (int) (num + 1090 * ratio));
                        } else if (screenHeight == 2016) {
                            mDevice.click((pointBeanList.get(i).getX()), (int) (num + 993 * ratio));
                        } else if (screenHeight == 1920) {
                            mDevice.click((pointBeanList.get(i).getX()), (int) (num + 1002 * ratio));
                        } else {
                            mDevice.click((pointBeanList.get(i).getX()), (int) (num + 840 * ratio));
                        }
                        showMsg("点击第" + j + "位");
                    }
                    break;
                }
            }
        }
        sleep(1000);
    }

    /**
     * 535型号U盾密码切换
     *
     * @param pad_ U盾密码
     * @throws UiObjectNotFoundException
     */
    public void getPwd_u_all(String pad_) {
        showMsg("===识别U盾开始888===");
        char[] pwd = pad_.toCharArray();//char数组
        String abc = splitNotNumber(pad_);
        showMsg("===U盾密码为===");
        String num = getNumbers(pad_);
        isAbcNum();
        if (isNumeric(String.valueOf(pwd[0]))) {
            //切换数字键盘
            if (screenHeight == 2102) {
                mDevice.click((int) (647 * ratio), (int) (1334 * ratio));
            } else if (screenHeight == 2016) {
                mDevice.click((int) (647 * ratio), (int) (1278 * ratio));
            } else if (screenHeight == 1920) {
                mDevice.click((int) (647 * ratio), (int) (1220 * ratio));
            } else {
                mDevice.click((int) (647 * ratio), (int) (1212 * ratio));
            }
            sleep(ChatClient.overtime_2);
            getNum_U_all(num);
            showMsg("===数字键盘识别完成=====");
            sleep(ChatClient.overtime_2);
            //切换字母键盘
            if (screenHeight == 2102) {
                mDevice.click((int) (647 * ratio), (int) (1334 * ratio));
            } else if (screenHeight == 2016) {
                mDevice.click((int) (647 * ratio), (int) (1278 * ratio));
            } else if (screenHeight == 1920) {
                mDevice.click((int) (647 * ratio), (int) (1220 * ratio));
            } else {
                mDevice.click((int) (647 * ratio), (int) (1212 * ratio));
            }
            sleep(1000);
            getABC_U_all(abc);
            showMsg("===字母键盘识别完成=====");
        } else {
            sleep(ChatClient.overtime_2);
            getABC_U_all(abc);
            showMsg("===字母键盘识别完成=====");
            //切换数字键盘
            if (screenHeight == 2102) {
                mDevice.click((int) (647 * ratio), (int) (1334 * ratio));
            } else if (screenHeight == 2016) {
                mDevice.click((int) (647 * ratio), (int) (1278 * ratio));
            } else if (screenHeight == 1920) {
                mDevice.click((int) (647 * ratio), (int) (1220 * ratio));
            } else {
                mDevice.click((int) (647 * ratio), (int) (1212 * ratio));
            }
            sleep(ChatClient.overtime_2);
            getNum_U_all(num);
            showMsg("===数字键盘识别完成=====");
        }

    }

    /**
     * 判断字母还是数字键盘
     */
    public void isAbcNum() {
        showMsg("===U盾识别=1====");
        try {
            boolean state = false;
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_15, "字母转换大写", Constants.notReport);
                if (isOutTime) {
                    addWorkTime(ChatClient.overtime_30);
                    if (screenHeight == 2102) {
                        mDevice.click((int) (40 * ratio), (int) (1340 * ratio));
                    } else if (screenHeight == 2016) {
                        mDevice.click((int) (40 * ratio), (int) (1278 * ratio));
                    } else if (screenHeight == 1920) {
                        mDevice.click((int) (40 * ratio), (int) (1220 * ratio));
                    } else {
                        mDevice.click((int) (40 * ratio), (int) (1212 * ratio));
                    }
                    sleep(ChatClient.overtime_1);
                    ChatClient.begintime = System.currentTimeMillis();//开始时间
                }
                if (!state) {
                    List<PointBean> pointBeanList;
                    sleep(ChatClient.overtime_1);
                    Bitmap bitmap = null;
                    if (screenHeight == 2102) {
                        bitmap = ImageView.getImage1(0, (int) (1080 * ratio), (int) (720 * ratio), (int) (1384 * ratio), (int) (720 * ratio), (int)
                                (304 * ratio));
                    } else if (screenHeight == 2016) {
                        bitmap = ImageView.getImage1(0, (int) (982 * ratio), (int) (720 * ratio), (int) (1344 * ratio), (int) (720 * ratio), (int)
                                (362 * ratio));
                    } else if (screenHeight == 1920) {
                        bitmap = ImageView.getImage1(0, (int) (990 * ratio), (int) (720 * ratio), (int) (1274 * ratio), (int) (720 * ratio), (int)
                                (189 * ratio));
                    } else {
                        bitmap = ImageView.getImage1(0, (int) (840 * ratio), (int) (720 * ratio), (int) (1280 * ratio), (int) (720 * ratio), (int)
                                (440 * ratio));
                    }
                    showMsg("===saveImg22===");
                    pointBeanList = saveImg22(bitmap, BANK_FLAG);
                    String tempStr = "";
                    if (pointBeanList.size() > 0) {
                        state = true;
                        for (int i = 0; i < pointBeanList.size(); i++) {
                            tempStr += pointBeanList.get(i).getCharStr();
                        }
                        showMsg("===识别结果键盘===" + tempStr);
                        char[] abc_num = tempStr.toCharArray();//字母或者数字数组
                        String num = String.valueOf(abc_num[0]);
                        if (isNumeric(num)) {
                            //切换数字键盘
                            if (screenHeight == 2102) {
                                mDevice.click((int) (647 * ratio), (int) (1334 * ratio));
                            } else if (screenHeight == 2016) {
                                mDevice.click((int) (647 * ratio), (int) (1278 * ratio));
                            } else if (screenHeight == 1920) {
                                mDevice.click((int) (647 * ratio), (int) (1220 * ratio));
                            } else {
                                mDevice.click((int) (647 * ratio), (int) (1212 * ratio));
                            }
                            sleep(1000);
                            showMsg("===默认切换到字母键盘=====");
                            isAbcNum1();
                            break;
                        } else {
                            if (tempStr.contains("y") || tempStr.contains("h") || tempStr.contains("m")) {
                                showMsg("===识别结果键盘包含=====yjm");
                                if (screenHeight == 2102) {
                                    mDevice.click((int) (40 * ratio), (int) (1340 * ratio));
                                } else if (screenHeight == 2016) {
                                    mDevice.click((int) (40 * ratio), (int) (1278 * ratio));
                                } else if (screenHeight == 1920) {
                                    mDevice.click((int) (40 * ratio), (int) (1220 * ratio));
                                } else {
                                    mDevice.click((int) (40 * ratio), (int) (1212 * ratio));
                                }
                                break;
                            } else if (tempStr.contains("Y") || tempStr.contains("H") || tempStr.contains("M")) {
                                break;
                            }
                        }
                    } else {
                        addWorkTime(30000);
                        if (screenHeight == 2102) {
                            mDevice.click((int) (40 * ratio), (int) (1340 * ratio));
                        } else if (screenHeight == 2016) {
                            mDevice.click((int) (40 * ratio), (int) (1278 * ratio));
                        } else if (screenHeight == 1920) {
                            mDevice.click((int) (40 * ratio), (int) (1220 * ratio));
                        } else {
                            mDevice.click((int) (40 * ratio), (int) (1212 * ratio));
                        }
                        sleep(1000);
                        showMsg("===无法识别===1111==");
                        state = false;
                    }
                }
            }
        } catch (Exception e) {
            setDevicesLog(e.getCause().getMessage());
            e.printStackTrace();
        }

    }

    /**
     * 判断字母还是数字
     */
    public void isAbcNum1() {
        try {
            showMsg("===U盾识别=2====");
            boolean state1 = false;
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_15, "字母转换大写", false);
                if (isOutTime) {
                    addWorkTime(30000);
                    if (screenHeight == 2102) {
                        mDevice.click((int) (40 * ratio), (int) (1340 * ratio));
                    } else if (screenHeight == 2016) {
                        mDevice.click((int) (40 * ratio), (int) (1278 * ratio));
                    } else if (screenHeight == 1920) {
                        mDevice.click((int) (40 * ratio), (int) (1220 * ratio));
                    } else {
                        mDevice.click((int) (40 * ratio), (int) (1212 * ratio));
                    }
                    sleep(1000);
                    ChatClient.begintime = System.currentTimeMillis();//开始时间
                    showMsg("===字母转换大写===");
                }
                if (!state1) {
                    sleep(1000);
                    List<PointBean> pointBeanList;
                    Bitmap bitmap = null;
                    if (screenHeight == 2102) {
                        bitmap = ImageView.getImage1(0, (int) (1080 * ratio), (int) (720 * ratio), (int) (1384 * ratio), (int) (720 *
                                ratio), (int) (304 * ratio));
                    } else if (screenHeight == 2016) {
                        bitmap = ImageView.getImage1(0, (int) (982 * ratio), (int) (720 * ratio), (int) (1344 * ratio), (int)
                                (720 * ratio), (int) (362 * ratio));
                    } else if (screenHeight == 1920) {
                        bitmap = ImageView.getImage1(0, (int) (990 * ratio), (int) (720 * ratio), (int) (1274 * ratio), (int)
                                (720 * ratio), (int) (189 * ratio));
                    } else {
                        bitmap = ImageView.getImage1(0, (int) (840 * ratio), (int) (720 * ratio), (int) (1280 * ratio), (int)
                                (720 * ratio), (int) (440 * ratio));
                    }
                    pointBeanList = saveImg22(bitmap, BANK_FLAG);
                    String tempStr = "";
                    if (pointBeanList.size() > 0) {
                        state1 = true;
                        for (int i = 0; i < pointBeanList.size(); i++) {
                            tempStr += pointBeanList.get(i).getCharStr();
                        }
                        showMsg("===识别结果键盘====" + tempStr);
                        if (tempStr.contains("y") || tempStr.contains("h") || tempStr.contains("m")) {
                            showMsg("===识别结果键盘包含=====yjm");
                            if (screenHeight == 2102) {
                                mDevice.click((int) (40 * ratio), (int) (1340 * ratio));
                            } else if (screenHeight == 2016) {
                                mDevice.click((int) (40 * ratio), (int) (1278 * ratio));
                            } else if (screenHeight == 1920) {
                                mDevice.click((int) (40 * ratio), (int) (1220 * ratio));
                            } else {
                                mDevice.click((int) (40 * ratio), (int) (1212 * ratio));
                            }
                            break;
                        } else if (tempStr.contains("Y") || tempStr.contains("H") || tempStr.contains("M")) {
                            break;
                        }
                    } else {
                        addWorkTime(30000);
                        if (screenHeight == 2102) {
                            mDevice.click((int) (40 * ratio), (int) (1340 * ratio));
                        } else if (screenHeight == 2016) {
                            mDevice.click((int) (40 * ratio), (int) (1278 * ratio));
                        } else if (screenHeight == 1920) {
                            mDevice.click((int) (40 * ratio), (int) (1220 * ratio));
                        } else {
                            mDevice.click((int) (40 * ratio), (int) (1212 * ratio));
                        }
                        sleep(1000);
                        showMsg("===无法识别====");
                        state1 = false;
                    }
                }
            }
        } catch (Exception e) {
            setDevicesLog(e.getCause().getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 字母和数字键盘的切换
     *
     * @param checkStr
     * @return
     */
    public static boolean isNumeric(String checkStr) {
        try {
            Integer.parseInt(checkStr);
            return true;
        } catch (NumberFormatException err) {
            return false;
        }
    }

    // 截取数字
    public static String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }

    // 截取非数字
    public static String splitNotNumber(String content) {
        Pattern pattern = Pattern.compile("\\D+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }

    /**
     * 弹框信息判断
     */
    private boolean isErrorInfo(String s) {
        boolean b = false;
        if (s.contains("转入账户与转出账户不能相同")) {
            LogToFile.v("======", "=======转入账户与转出账户不能相同=====");
            b = true;
        }
        return b;
    }

    /**
     * 遇到网络异常时继续执行
     */
    public void setContinue(String name) throws UiObjectNotFoundException {
        addWorkTime(ChatClient.overtime_30);
        if ("Login".equals(name)) {//点击登录
            //登录
            mDevice.click((int) (355 * ratio), (int) (915 * ratio));
            CCBMethodName = "Login";
        } else if ("setTimerun".equals(name)) {//进入转账
            mDevice.click((int) (420 * ratio), (int) (545 * ratio));
            Constants.CODE = "";
            ChatClient.request();
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_90, "未查找到弹窗", Constants.isReport);
                if (isOutTime) {
                    return;
                }
                UiObject zhuanzhang = CodeUtils.getUiObject_textOrId("确定", "android:id/button1");
                if (zhuanzhang.exists()) {
                    zhuanzhang.click();
                    break;
                }
            }
            CCBMethodName = "setTimerun";
        } else if ("remittanceLongin".equals(name)) {//进入转账
            //进入下一步
            sleep(1000);
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_90, "未查找到《下一步》", Constants.isReport);
                if (isOutTime) {
                    return;
                }
                UiObject click = CodeUtils.getUiObject_textOrClass("下一步", "android.widget.Button");
                if (click.exists()) {
                    click.click();
                    break;
                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            CCBMethodName = "remittanceLongin";
        } else if ("Submit".equals(name)) {//确认转账
            sleep(2000);
            //确认转账
            ChatClient.begintime = System.currentTimeMillis();//开始时间
            while (true) {
                long nowtime = System.currentTimeMillis();
                if ((nowtime - ChatClient.begintime) > ChatClient.overtime) {
                    ChatClient.timer("setContinue", ChatClient.errorStr);
                    return;
                }
                UiObject ok = mDevice.findObject(new UiSelector().text("确定").className("android.widget.Button"));
                UiObject ok1 = mDevice.findObject(new UiSelector().text("确认转账").className("android.widget.Button"));
                if (ok1.exists()) {
                    ok1.click();
                    break;
                } else if (ok.exists()) {
                    ok.click();
                }
            }
            CCBMethodName = "Submit";
        } else if ("Re_send".equals(name)) {
        }
    }

    /**
     * 输入U盾密码
     *
     * @throws UiObjectNotFoundException
     */
    public void setUpwd() throws UiObjectNotFoundException {
        UiObject zhuanzhang = CodeUtils.getUiObject_textOrClass("继续转账", "android.widget.Button");
        if (zhuanzhang.exists()) {
            return;
        }
        addWorkTime((int) ChatClient.overtime_20);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_90, "链接超时...", Constants.isReport);
            if (isOutTime) {
                return;
            }
            UiObject upwd1 = CodeUtils.getUiObject_contentText("屏显翻转");
            UiObject upwd2 = CodeUtils.getUiObject_contentText("核对K宝");
            UiObject zhuanzhangnext = CodeUtils.getUiObject_textOrClass("继续转账", "android.widget.Button");
            if (upwd1.exists()) {
                showMsg("---屏显翻转---");
                break;
            } else if (upwd2.exists()) {
                showMsg("---核对K宝---");
                break;
            } else if (zhuanzhangnext.exists()) {
                showMsg("---继续转账---");
                break;
            }
            boolean isOutTime1 = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_8, "开始输入U盾密码", Constants.notReport);
            if (isOutTime1) {
                sleep(ChatClient.overtime_2);
                showMsg("===U盾密码输入开始===");
                if (isNewU) {
                    clickUPwd(user.getUsbPayPwd());
                } else {
                    getPwd_u_all(user.getUsbPayPwd());
                }
                showMsg("===U盾密码输入完成===");
                ChatClient.begintime = System.currentTimeMillis();//开始时间
                while (true) {
                    boolean isOutTime2 = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_90, "确认超时...", Constants.isReport);
                    if (isOutTime2) {
                        return;
                    }
                    UiObject upwd = CodeUtils.getUiObject_contentText("确定");
                    UiObject upwdt = mDevice.findObject(new UiSelector().index(1).className("android.widget.Button"));
                    UiObject upwdcancel = mDevice.findObject(new UiSelector().text("取消").className("android.widget.Button"));
                    if (upwdt.exists() && upwdcancel.exists()) {
                        upwdt.click();
                        showMsg("upwdt----点击确定1----");
                        break;
                    } else if (upwd.exists()) {
                        upwd.click();
                        showMsg("upwd----点击确定2----");
                        break;
                    }
                }

                sleep(ChatClient.overtime_3);
                while (true) {
                    boolean isOutTime2 = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_3, "确认超时...", Constants.notReport);
                    if (isOutTime2) {
                        break;
                    }
                    UiObject qx = CodeUtils.getUiObject_textOrClass("取消", "android.widget.Button");
                    if (qx.exists()) {
                        qx.click();
                        sleep(ChatClient.overtime_3);
                        setUBack();
                        sleep(ChatClient.overtime_2);
                        addWorkTime(ChatClient.overtime_60);
                        showMsg("===未检测到U盾===");
                        setNext();
                        break;
                    }
                }
                break;
            }
        }
        sleep(ChatClient.overtime_2);
        pressu("2");
    }

    /**
     * 新版U盾935型点击密码
     *
     * @param usbPayPwd
     */
    private void clickUPwd(String usbPayPwd) {
        showMsg("新版U盾点击密码：点击切换字母键盘");
        mDevice.click((int) (620 * ratio), (int) (1280 * ratio));
        sleep(ChatClient.overtime_3);
        showMsg("点击切换大写字母");
        mDevice.click((int) (47 * ratio), (int) (1280 * ratio));
        sleep(ChatClient.overtime_3);
        List<PointBean> pointBeanList = get_U_Key_XY(BANK_FLAG + "935A");
        boolean isNum = false;
        char[] chars = usbPayPwd.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            String s = String.valueOf(chars[i]);
            if (isNumeric(s) && !isNum) {
                showMsg("点击切换数字键盘");
                mDevice.click((int) (633 * ratio), (int) (1267 * ratio));
                sleep(ChatClient.overtime_3);
                pointBeanList = get_U_Key_XY(BANK_FLAG + "935N");
                isNum = true;
            }
            for (PointBean point : pointBeanList) {
                if (point.getCharStr().equals(s)) {
                    showMsg("点击第" + i + "位");
                    mDevice.click((int) (17 * ratio) + (point.getX()), (int)
                            (1017 * ratio) + point.getY());
                    sleep(1000);
                    break;
                }
            }
        }
    }


    /**
     * 新款935型U盾键盘截图
     */
    public List<PointBean> get_U_Key_XY(String bankname) {
        List<PointBean> pointBeanList;
        Bitmap bitmap = null;
        if (screenHeight == 2016) {
            showMsg("截图新U盾键盘");
            bitmap = ImageView.getImage1((int) (17 * ratio), (int) (1017 * ratio), (int) (700 * ratio), (int)
                    (1323 * ratio), (int) (700 * ratio) - (int) (17 * ratio), (int) (1323 * ratio) - (int) (1017 * ratio));
        } else if (screenHeight == 1920) {

        } else {

        }

        pointBeanList = saveImg22(bitmap, bankname);
        return pointBeanList;
    }

    /**
     * 确认转账
     *
     * @throws UiObjectNotFoundException
     */
    public void setNext() throws UiObjectNotFoundException {
        showMsg(">>>>>确认转账");
        //进入转账页面
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_40, "setNext", Constants.isReport);
            if (isOutTime) {
                return;
            }
            UiObject ok = mDevice.findObject(new UiSelector().text("确定").className("android.widget.Button"));
            UiObject ok1 = mDevice.findObject(new UiSelector().text("确认转账").className("android.widget.Button"));

            UiObject zhuanzhang = CodeUtils.getUiObject_textOrClass("继续转账", "android.widget.Button");

            UiObject upwd14 = CodeUtils.getUiObject_textOrClass("请核对K宝显示的内容是否正确，如确认请按“OK”键（或“√”键），取消请按“C”键（或“×”键）", "android.widget.TextView");
            UiObject upwd15 = CodeUtils.getUiObject_textOrClass("请核对K宝显示内容，如确认请按K宝的[OK]键，取消请按K宝的[C]键。", "android.widget.TextView");

            UiObject upwd5_1 = CodeUtils.getUiObject_containTextOrClass("请核对K宝显示内容", "android.widget.TextView");
            UiObject upwd8 = CodeUtils.getUiObject_containTextOrClass("K宝屏显翻转", "android.widget.TextView");

            if (ok.exists()) {
                ok.click();
                showMsg("点击确认");
                break;
            } else if (ok1.exists()) {
                ok1.click();
                showMsg("点击确认转账");
                break;
            }
            if (upwd14.exists() || upwd8.exists() || upwd15.exists() || upwd5_1.exists()) {
                sleep(ChatClient.overtime_2);
                pressu("2");
                break;
            }
            if (zhuanzhang.exists()) {
                return;
            }
        }
        showMsg("查找继续转账");
        sleep(ChatClient.overtime_3);
        while (true) {
            UiObject zhuanzhang = CodeUtils.getUiObject_textOrClass("继续转账", "android.widget.Button");
            UiObject ProgressBar = mDevice.findObject(new UiSelector().className("android.widget.ProgressBar"));
            if (ProgressBar.exists()) {
            } else {
                showMsg(">>>>>检查U盾进度条");
                break;
            }
            if (zhuanzhang.exists()) {
                // getUserInfo(sumMoney);
                showMsg(">>>>>查继续转账");
                return;
            }
        }

        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            boolean isOutTime = CodeUtils.outTime(ChatClient.begintime, ChatClient.overtime_5, "关闭K宝链接失败弹窗超时", Constants.notReport);
            if (isOutTime) {
                break;
            }
            UiObject mTV = mDevice.findObject(new UiSelector().textContains("连接失败"));
            if (mTV.exists()) {
                showMsg("K宝链接失败");
                UiObject mBT = mDevice.findObject(new UiSelector().className("android.widget.Button").text("确定").index(0));
                if (mBT.exists()) {
                    showMsg("关闭K宝链接失败弹窗");
                    mBT.click();
                    sleep(ChatClient.overtime_2);
                    setNext();
                    break;
                }
            }
        }
        sleep(ChatClient.overtime_2);
        ChatClient.begintime = System.currentTimeMillis();//开始时间
    }

    public void setUBack() throws UiObjectNotFoundException {
        while (true) {
            UiObject ok = mDevice.findObject(new UiSelector().text("确定").className("android.widget.Button"));
            if (ok.exists()) {
                sleep(ChatClient.overtime_3);
                ok.click();
                break;
            }
        }
    }

    /**
     * 发送转账失败
     */
    private void sendUnBankInfo(String info) {
        ChatClient.reportTransferOrder(BANK_NAME, info, sumMoney, 1);
    }


    private boolean uiLayoutDes(UiObject2 ui) {
        UiObject2 layout = ui.getChildren().get(0).getChildren().get(0).getChildren().get(0);
        List<UiObject2> layouts = layout.getChildren();
        showMsg("layouts size = " + layouts.size());
        int index = 0;
        for (UiObject2 u2 : layouts) {
            index++;
            showMsg("u2 = " + u2.getText() + " ===========index= " + index);
            if (!TextUtils.isEmpty(u2.getText()) && u2.getText().equals(ZZTAG)) {
                targetLayout = layout;
                return true;
            }
        }
        return false;
    }

    public void showMsg(String msg) {
        LogToFile.showLog("农业银行", msg);
    }

    @Override
    public void keyPhone(int screenHeight) {

    }

    @Override
    public void keyPhone(String device) {

    }
}